import pinyin from 'pinyin';

interface Options {
  keyName: string;
}

interface DefaultOrigin {
  [key: string]: string;
}

/*
 * 拼入拼音对象
 */
export function wrapPinyin<T extends DefaultOrigin>(origin: T, options: Options) {
  const label: string = origin[options.keyName];
  const pinyinList: string[] = pinyin(label, {
    style: pinyin.STYLE_INITIALS, // 设置拼音风格
    heteronym: false,
  });
  const spelling = pinyinList.join('');
  return {
    origin,
    pinyin: {
      list: pinyinList,
      spelling,
      initial: spelling.slice(0, 1),
    },
  };
}


/**
 *  包裹列表
 */
export function eachWrap<T extends DefaultOrigin>(list: T[], options: Options) {
  return list.map((item: T) => wrapPinyin(item, options));
}
